$(
    function() {
        //把三级联动的神奇添加点击事件，实例化三级联动的组件填充，获取数据

        let isEdit = Number(getParamsByUrl(location.href, "isEdit"));
        console.log(isEdit);
        let address;
        if (isEdit) {
            console.log("isEdit成功");
            console.log(localStorage.getItem("editAddress"));

            if (localStorage.getItem("editAddress")) {
                address = JSON.parse(localStorage.getItem("editAddress"));
                console.log(address);
                let html = template("editTpl", address);
                $("#editForm").html(html);
            }
        } else {
            let html = template("editTpl", {});
            $("#editForm").html(html);
        }
        let picker = new mui.PopPicker({ layer: 3 });
        picker.setData(cityData);
        // console.log(cityData);



        //weishuru
        $("#selectCity").on("tap", function() {
            picker.show(function(selectItems) {
                $("#selectCity").val(selectItems[0].text + selectItems[1].text + selectItems[2].text);
                console.log(selectItems[0].text);
                console.log(selectItems[1].text);
                console.log(selectItems[2].text);
            })
        })

        $("#addAddress").on("tap", function() {
            let username = $.trim($("[name='username']").val());
            let postCode = $.trim($("[name='postCode']").val());
            let city = $.trim($("[name='city']").val());
            let detail = $.trim($("[name='detail']").val());

            console.log("username:" + username);
            console.log("postCode:" + postCode);
            console.log("city:" + city);
            console.log("detail:" + detail);
            console.log("------------------");
            if (!username) {
                mui.toast("请输入收货人姓名");
                return;
            }
            if (!postCode) {
                mui.toast("请输入邮编");
                return;
            }
            if (!city) {
                mui.toast("请选择城市");
                return;
            }
            if (!detail) {
                mui.toast("请输入详细地址");
                return;
            }
            let data = {
                recipients: username,
                postcode: postCode,
                address: city,
                addressDetail: detail
            }
            let url;
            if (isEdit) {
                url = "/address/updateAddress";
                data.id = address.id;
                console.log("address" + address);
            } else {
                url = "/address/addAddress";
            }
            $.ajax({
                url: url,
                type: "POST",
                data: data,
                success: function(res) {

                    if (res.success) {
                        if (isEdit) {
                            mui.toast("修改地址成功");
                        } else {
                            mui.toast("添加收货地址成功");

                        }
                        setTimeout(function() {
                            location.href = "address.html"
                        }, 2000);


                        console.log(res);
                        console.log(data)
                        console.log(res.recipients);
                        console.log(res.postcode);
                        console.log(res.address);
                        console.log(res.addressDetail);
                    }
                }
            });

        })
    }
)


function getParamsByUrl(url, name) {
    let params = url.substr(url.indexOf("=") + 1);
    return params;
}